package com.dhcc.sds.base.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.Condition;
import com.dhcc.sds.base.entity.SdsDicEntity;
import com.dhcc.sds.base.entity.SdsDictypeEntity;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsDicService;
import com.dhcc.sds.base.service.ISdsDictypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.base.entity.SdsExpressEntity;
import com.dhcc.sds.base.service.ISdsExpressService;


/**
 * 产品函数库
 * 
 * @ClassName: SdsExpressController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:10:53
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsexpress")
public class SdsExpressController extends BaseController {

	private String PREFIX = "/backend/sds/sdsexpress/";

	@Autowired
	private ISdsExpressService sdsExpressService;
	@Autowired
    private ISdsCommonService sdsCommonService;

    /**
     * 跳转到产品函数库列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        JSONObject expType = sdsCommonService.getDicMap("SDExpType");
        model.addAttribute("expType", expType);
        return PREFIX + "sdsexpress.html";
    }
	
    /**
     * 跳转到新增产品函数库
     */
    @RequestMapping("/to_add")
    public String toAdd(Model model) {
        JSONObject expType = sdsCommonService.getDicMap("SDExpType");
        model.addAttribute("expType", expType);
        return PREFIX + "sdsexpress_add.html";
    }
	
	/**
     * 跳转到修改产品函数库
     */
    @RequestMapping("/to_update/{sdsExpressId}")
    public String toUpdate(@PathVariable Long sdsExpressId, Model model) {
        SdsExpressEntity sdsExpress = this.sdsExpressService.selectById(sdsExpressId);
        model.addAttribute("sdsExpress",sdsExpress);
        JSONObject expType = sdsCommonService.getDicMap("SDExpType");
        model.addAttribute("expType", expType);
        return PREFIX + "sdsexpress_edit.html";
    }

    /**
     * 获取产品函数库列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsExpressEntity> page = new PageFactory<SdsExpressEntity>().defaultPage();
        List<SdsExpressEntity> result = this.sdsExpressService.page(page,params);
        page.setRecords((List<SdsExpressEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增产品函数库
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsExpressEntity sdsExpress) {
        if (CommonUtil.isOneEmpty(sdsExpress)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsExpress.insert();
        return R.ok();
    }

    /**
     * 删除产品函数库
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsExpressId) {
	
        this.sdsExpressService.deleteById(sdsExpressId);

        return R.ok();
    }

    /**
     * 修改产品函数库
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsExpressEntity sdsExpress) {
        if (CommonUtil.isOneEmpty(sdsExpress)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsExpressEntity old = this.sdsExpressService.selectById(sdsExpress.getId());

        old.setExpCode(sdsExpress.getExpCode());
        old.setExpDesc(sdsExpress.getExpDesc());
        old.setExpType(sdsExpress.getExpType());
        old.setExpress(sdsExpress.getExpress());
        old.setParamResume(sdsExpress.getParamResume());
        old.setExpResume(sdsExpress.getExpResume());
        old.setIsActive(sdsExpress.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
}
